#ifndef BOT_H
#define BOT_H

#include "movingobject.h"
#include "world.h"
#include "astar.h"
#include <iostream>
#include <QtGui>
#include <QGraphicsScene>
#include <QPen>
#include <QColor>


class World;
class ZombieMaster;

class Bot: public MovingObject
{

private:


    Path* path;
    AStar* m_astarSearch;
    QRectF m_boundingRect;
    World* m_world;
    QVector2D m_targetPos;
    QPointF m_oldBotPos;
    ZombieMaster *master;
    int firerate;

    bool m_stop;





public:

    QPointF m_faja;

    Node* nodePos;
    int index;
    int nextNodeIndex;
    Bot(World* w, Path* p, Graph *g);
    Bot(){};
    ~Bot();


    void addMaster(ZombieMaster* m)
    {
        master=m;
    }

    void paint(QPainter *, const QStyleOptionGraphicsItem *,QWidget *);
    inline const World* world()const;

    void setTargetPos(QVector2D);
    QPointF getOldBotPos();
    void seek(const QVector2D&);
    void flee(const QVector2D&);
    void arrive (const QVector2D&);

    void ObstacleAvoidance();

    void stop();
    void start();
    void step();
    void fire();


    void setBoundingRect(QRectF);
};
class Zombie;

class ZombieMaster: public Bot
{

private:
    QList<Zombie*> zombies;
    World* m_world;
    Path* path;
    AStar* m_astarSearch;
    QRectF m_boundingRect;
    Bot* bot;
    QVector2D m_targetPos;

public:
    int index;
    ZombieMaster(World* w, Path* p, Graph *g, Bot* b);
    void paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*);
    void report(Zombie* z);
    void attack();
    void step();
    void setTargetPos(QVector2D p);


};



class Zombie : public Bot
{

private:
    ZombieMaster* master;
    Path* path;
    AStar* m_astarSearch;
    QRectF m_boundingRect;
    World* m_world;
    QVector2D m_targetPos;
    bool raport;
    bool m_attack;
    Bot* bot;


public:
    Zombie(World* w, Path* p, Graph *g, ZombieMaster* m):master(m),Bot(w,p,g)
    {

        raport=false;
        m_attack=false;
        path=p;
        m_world=w;
        m_velocity=QVector2D(1.0,0);
        m_heading=QVector2D(1,0);
        nodePos=g->getNode(rand()%1000);
        this->setPos(nodePos->getPos());



        nextNodeIndex=0;


        m_astarSearch=new AStar(g,new Euclides );//new ZeroHeuristic());
        m_astarSearch->setSource(nodePos->getIndex());

        index=nodePos->getIndex();

        m_astarSearch->setTarget(master->index);
        m_astarSearch->SearchPath();
        m_astarSearch->ConstructPath();

        path->newPath(m_astarSearch->newPath());

        setTargetPos(QVector2D(path->getNodePos(0).x(),path->getNodePos(0).y()));
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
    {

        painter->setPen(QPen(Qt::black));
        painter->setBrush(Qt::green);
        painter->drawEllipse(0,-10,20,20);
    }
    void setBot(Bot* b)
    {
        bot=b;
    }
    void attack()
    {
        m_attack=true;

    }

    void step()
    {



        if(m_attack && index!=bot->index)
        {


            m_astarSearch->setSource(index);

            m_astarSearch->setTarget(bot->index);
            m_astarSearch->SearchPath();
            m_astarSearch->ConstructPath();

            path->newPath(m_astarSearch->newPath());
            nextNodeIndex=0;
            setTargetPos(QVector2D(path->getNodePos(0).x(),path->getNodePos(0).y()));
            index=path->m_patch[0]->getFrom()->getIndex();
        }




        double eps=10.00001;

        seek(m_targetPos);
        float len=(m_targetPos.x()-this->scenePos().x())*(m_targetPos.x()-this->scenePos().x());
        len+=(m_targetPos.y()-this->scenePos().y())*(m_targetPos.y()-this->scenePos().y());


        if(nextNodeIndex<path->m_patch.size()-2 && len <eps )
        {
            ++nextNodeIndex;
            setTargetPos(QVector2D(path->getNodePos(nextNodeIndex).x(),path->getNodePos(nextNodeIndex).y()));
            index=path->m_patch[nextNodeIndex]->getFrom()->getIndex();
        }
        else if(!raport&& nextNodeIndex>=path->m_patch.size()-2)
        {
            master->report(this);
            raport=true;

        }



        this->setPos(this->scenePos().x()+m_velocity.toPoint().x()*2,this->scenePos().y()+m_velocity.toPoint().y()*2);











    }

    void setTargetPos(QVector2D p)
    {
        m_targetPos=p;
    }

};


#endif // BOT_H
